package com.hitrust.gs.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import system.data.CompanyMessageData;
import system.data.ProductTaxData;

import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.lib.SQLLogger;
import com.hitrust.gs.lib.db.GSBaseDAO;
import com.hitrust.gs.lib.db.GSPreparedStatementFactory;
import com.hitrust.gs.util.exception.GSException;

public class GSProductTaxDao extends GSBaseDAO{
	public List queryProductTax() throws GSException{
    	List ls = null;
        String tSql = "select * from t_product_tax";
        Object tObj[] = null;
        try{
            ls = executeQuery(tSql,tObj);          
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return ls;
    }
	
	/**
     * 查询产品是否已经有税务信息
     * @throws GSException
     */
    public List queryProductTaxIsOk(String productCode) throws GSException{
    	List ls = null;
        String tSql = "select * from t_product_tax where product_code = ?";
        Object tObj[] = {productCode};
        try{
            ls = executeQuery(tSql,tObj);          
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return ls;
    }
	
	
	/**
     * 查询某个产品税务信息
     * @throws GSException
     */
    public List queryProductTaxOne(String productId) throws GSException{
    	List ls = null;
        String tSql = "select a1.*,a2.name,a2.concentricity_code from t_product a2 left join t_product_tax a1 on a2.concentricity_code = a1.product_code  where a2.id = ?";
        Object tObj[] = {productId};
        try{
            ls = executeQuery2(tSql,tObj);          
        }catch(Exception e){
            GSLogWriter.error(this, e.toString());
            throw new GSException(GSException.E19999, GSException.E19999_MSG);
        }
        return ls;
    }
	
	public ArrayList executeQuery( String aSql,
			Object[] aStmntValues) throws SQLException, Exception {
		try {
			iConn = this.getConnetion();
			SQLLogger.log(aSql, aStmntValues);
			iPstmt = iConn.prepareStatement(aSql);

			// 组合Statement条件.
			if (aStmntValues != null) {
				GSPreparedStatementFactory.buildStatement(iPstmt, aStmntValues);
			}

			// 执行
			iRs = iPstmt.executeQuery();

			ArrayList tArrayList = new ArrayList();
			while (iRs.next()) {
				ProductTaxData aBean= new ProductTaxData();
				aBean.setId(iRs.getString("id"));
				aBean.setProductCode(iRs.getString("product_code"));
				aBean.setCodeEdition(iRs.getString("code_edition"));
				aBean.setTaxType(iRs.getString("tax_type"));
				//String brand = iRs.getString("product_brand");
				tArrayList.add(aBean);
			}
			SQLLogger.log("Success");
			// 返回结果数组列表
			return tArrayList;
		} catch (SQLException e) {
			SQLLogger.log("error:" + e.toString());
			pushSQLError(e.toString());
			GSLogWriter.debug(this, e);
			GSLogWriter.debug(this, aSql);
			throw e;
		} finally {
			this.close();
		}
	}
	
	//查询产品税务信息
	public ArrayList executeQuery2( String aSql,
			Object[] aStmntValues) throws SQLException, Exception {
		try {
			iConn = this.getConnetion();
			SQLLogger.log(aSql, aStmntValues);
			iPstmt = iConn.prepareStatement(aSql);

			// 组合Statement条件.
			if (aStmntValues != null) {
				GSPreparedStatementFactory.buildStatement(iPstmt, aStmntValues);
			}

			// 执行
			iRs = iPstmt.executeQuery();

			ArrayList tArrayList = new ArrayList();
			while (iRs.next()) {
				ProductTaxData aBean= new ProductTaxData();
				aBean.setId(iRs.getString("id"));
				aBean.setProductName(iRs.getString("name"));
				aBean.setProductCode(iRs.getString("concentricity_code"));
				aBean.setCodeEdition(iRs.getString("code_edition"));
				aBean.setTaxType(iRs.getString("tax_type"));
				tArrayList.add(aBean);
			}
			SQLLogger.log("Success");
			// 返回结果数组列表
			return tArrayList;
		} catch (SQLException e) {
			SQLLogger.log("error:" + e.toString());
			pushSQLError(e.toString());
			GSLogWriter.debug(this, e);
			GSLogWriter.debug(this, aSql);
			throw e;
		} finally {
			this.close();
		}
	}
	
	
	
	
	public boolean update(String productCode,ProductTaxData ptd) throws GSException {
        StringBuffer Sql = new StringBuffer();
        Sql.append("update t_product_tax set ");
        if(ptd.getCodeEdition()!=null&&!ptd.getCodeEdition().equals("")){
			Sql.append("code_edition='"+ptd.getCodeEdition()+"', ");
		}
        if(ptd.getTaxType()!=null&&!ptd.getTaxType().equals("")){
			Sql.append("tax_type='"+ptd.getTaxType()+"', ");
		}
        Sql.append("product_code='"+productCode+"' ");
        Sql.append("where product_code ='"+productCode+"' ");
        String tSql = Sql.toString();
        Object[] tObject = null;
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
	
	public boolean insert(ProductTaxData ptd) throws GSException {
        String tSql = "insert into t_product_tax(id, product_code, code_edition,tax_type) "+
        				"values(sys_guid(),'"+ptd.getProductCode()+"','"+ptd.getCodeEdition()+"','"+ptd.getTaxType()+"')";
        Object[] tObject = null;
        boolean tResult = false;
        try {
            tResult=executeUpdate(tSql, tObject);
        } catch (Exception ex) {
            GSLogWriter.error(this, ex.getMessage());
            throw new GSException(ex.getMessage());
        }

        return tResult;
    }
	
}
